//
//  NSDate+Custom.m
//  lifestyle
//
//  Created by wd on 15/9/9.
//  Copyright (c) 2015年 Wei Chuang Le ,Ltd. All rights reserved.
//

#import "NSDate+Custom.h"

@implementation NSDate (Custom)

- (NSInteger)year
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitYear;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:self];
    
    return theComponents.year;
}

- (NSInteger)getYear
{
    return self.year;
}

- (NSInteger)month
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSMonthCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:self];
    
    return theComponents.month;
}

- (NSInteger)getMonth
{
    return self.month;
}

- (NSInteger)day
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSDayCalendarUnit;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:self];
    
    return theComponents.day;
}

- (NSInteger)getDay
{
    return self.day;
}

- (NSInteger)daysCountForYearMonth
{
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
   
    NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:self];
    NSUInteger numberOfDaysInMonth = range.length;
    
    return numberOfDaysInMonth;
}

- (NSInteger)getDaysCountForYearMonth
{
    return self.daysCountForYearMonth;
}
@end
